package com.mjh.zhujianju.service.set.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.google.common.collect.Lists;
import com.mjh.zhujianju.util.FileUrlAdapter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.set.BanersMapper;
import com.mjh.zhujianju.enums.BanersEnum;
import com.mjh.zhujianju.model.set.BanersEO;
import com.mjh.zhujianju.service.set.BanersService;
import com.mjh.zhujianju.vo.set.BanersVO;
import org.springframework.util.CollectionUtils;

/**
 * @author liuyang
 * @Description:
 * @date 2018/8/8 23:58
 */
@Service
public class BanersServiceImpl extends BaseServiceImpl<BanersEO> implements BanersService {

    @Autowired
    private BanersMapper banersMapper;
    
    @Resource
    private FileService fileService;

    public BanersServiceImpl(BanersMapper banersMapper) {
        super(banersMapper);
    }

    @Override
    public List<BanersVO> queryBanersList(int site) {
    	List<BanersVO> list = Lists.newArrayList();
    	List<BanersEO> banersEOList = banersMapper.queryBanersList(site);
    	if (CollectionUtils.isEmpty(banersEOList)){
    		return list;
		}
		BanersVO banersVO;
		for (BanersEO banersEO : banersEOList){
			banersVO = new BanersVO();
			BeanUtils.copyProperties(banersEO,banersVO);
			list.add(banersVO);
		}

		List<Long> ids = list.stream().map(banersVO1 -> banersVO1.getId()).collect(Collectors.toList());

		List<FileEO> fileEOList = fileService.getList(ids,BanersEO.class.getName(),null);
		if (CollectionUtils.isEmpty(fileEOList)){
			return list;
		}

		for (BanersVO vo : list){
			for (FileEO fileEO : fileEOList){
				if (vo.getId().equals(fileEO.getCaseId())){
					vo.setFileAction(FileUrlAdapter.assemblyAction(fileEO.getId()));
					continue;
				}
			}
		}
        return list;
    }
    
    
    
    @Override
  	public void saveInfo(BanersVO vo) throws XTBusinessException {
  		BanersEO eo = new BanersEO();
  		BeanUtils.copyProperties(vo, eo);
  		eo.setStatus(EStatus.active.getCode());
//  		OrganizationEO org =	organizationService.get();
//  		if(org!=null) {
//  			eo.setDeptId(org.getId());
//  		}
  		this.save(eo);
  		if (!XTListUtils.isEmpty(vo.getFileIds())) {
  			fileService.updateList(vo.getFileIds(), eo.getId(), BanersEO.class.getName());
  		}
  		
  	}

  	@Override
  	@Transactional
  	public void updateInfo(BanersVO vo) throws XTBusinessException {
  		BanersEO eo = new BanersEO();
  		BeanUtils.copyProperties(vo, eo);
  		eo.setStatus(EStatus.active.getCode());
  		this.updateByPrimaryKeySelective(eo);
  		fileService.updateList(vo.getFileIds(), eo.getId(), BanersEO.class.getName());
  	}

  	@Override
  	public PagedResult<BanersVO> getVOPage(BanersVO vo) {
  		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
  		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
  		PageHelper.startPage(pageNo, pageSize);
  		List<BanersEO> list = banersMapper.selectPagination(vo);
  		PagedResult<BanersEO> page = PagedBeanUtil.toPagedResult(list);
  		PagedResult<BanersVO> result = new PagedResult<>();
  		BeanUtils.copyProperties(page, result);
  		if (!XTListUtils.isEmpty(list)) {
  			List<Long> ids = XTEntityUtils.getIdList(list);
  			//工作内容
  			List<BanersVO> voList = new ArrayList<BanersVO>();
  			Map<Long, FileEO> files = fileService.getMapTopOne(ids, BanersEO.class.getName(), null);
			for (BanersEO BanersEO : list) {
				BanersVO BanersVO =new BanersVO();
				BeanUtils.copyProperties(BanersEO, BanersVO);
				if(files!=null) {
				FileEO file = files.get(BanersVO.getId());
				if(file!=null) {
					BanersVO.getFileIds().add(file.getId());
				}
			}	
				BanersVO.setSiteName(BanersEnum.getName(BanersVO.getSite()));
				voList.add(BanersVO);
  			}
  			result.setDataList(voList);
  		}
  		return result;
  	}

  	@Override
  	public BanersVO getInfo(Long id) throws XTBusinessException{
  		BanersEO wd = super.getByPrimaryKey(id);
  		if (wd != null) {
  			BanersVO vo = new BanersVO();
  			BeanUtils.copyProperties(wd, vo);
  			List<FileEO> files = fileService.getFileById(id, BanersEO.class.getName(), null);
  			if(!XTListUtils.isEmpty(files)) {
  				vo.setFiles(files);
  		  		vo.setFileIds(XTEntityUtils.getIdList(files));
  			}
  			vo.setSiteName(BanersEnum.getName(vo.getSite()));
  			return vo;
  		}

  		return null;
  	}
  
}
